<template>
  <!--直播管理-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button type="primary" @click="add">添加</el-button>
        <!-- <el-button type="primary">模块状态：开启中</el-button> -->
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="title"
        label="名称"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="order"
        label="排序值"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="url"
        label="链接"
      />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope)">编辑</el-button>
          <el-button type="text" @click="delList(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加编辑 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="100px">
        <el-form-item label="标题:">
          <el-input v-model="form.title" placeholder="请输入标题" :rows="15" />
        </el-form-item>
        <el-form-item label="直播链接:">
          <el-input
            v-model="form.live_url"
            placeholder="请输入链接"
            :rows="15"
          />
        </el-form-item>
        <el-form-item label="排序值:">
          <el-input
            v-model="form.order"
            placeholder="请输入排序值,越大越在前面"
            :rows="15"
          />
        </el-form-item>
        <el-form-item label="上传封面:">
          <el-upload
            class="uploads"
            action=""
            list-type="picture-card"
            :show-file-list="false"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :http-request="uploadImg"
          >
            <img v-if="img" width="150px" :src="img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-show="addtype" type="primary" @click="send()">
          确 定
        </el-button>
        <el-button v-show="edittype" type="primary" @click="save()">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import {
    getLiveLists,
    addLiveDatas,
    delLiveDatas,
    editLiveDatas,
    getOssUploadsParam,
  } from '@/api/common'
  import axios from 'axios'

  export default {
    name: 'Live',
    data() {
      return {
        queryForm: {
          page: 1,
          list_row: 10,
        },
        form: {
          title: '',
          live_url: '',
          order: '',
        },
        disabled: false,
        listLoading: false,
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        wintit: '',
        liveId: '',
        img: '',
        addtype: false,
        edittype: false,
      }
    },
    created() {
      this.getData()
    },
    methods: {
      handleRemove() {},
      handlePictureCardPreview() {},
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      restart() {
        this.queryForm = []
        this.getData()
      },
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          keyword: '',
        }
        getLiveLists(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.addtype = true
        this.edittype = false
        this.form = []
        this.img = ''
      },
      edit(row) {
        this.dialogFormVisible = true
        this.wintit = '编辑'
        this.addtype = false
        this.edittype = true
        this.form = this.list[row.$index]
        this.img = this.list[row.$index].url
        this.linveId = row.row.id
      },
      send() {
        let params = {
          title: this.form.title,
          order: this.form.order,
          live_url: this.form.live_url,
          url: this.img,
        }
        addLiveDatas(params).then((data) => {
          this.$message.success('添加成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      save() {
        let params = {
          id: this.linveId,
          title: this.form.title,
          order: this.form.order,
          live_url: this.form.live_url,
          url: this.img,
        }
        editLiveDatas(params).then((data) => {
          this.$message.success('修改成功！')
          this.dialogFormVisible = false
          this.getData()
        })
      },
      delList(row) {
        let params = {
          id: row.id,
        }
        delLiveDatas(params).then((data) => {
          this.$message.success('删除成功！')
          this.getData()
        })
      },
      uploadImg(params) {
        const file = params.file,
          fileType = file.type,
          isImage = fileType.indexOf('image') != -1,
          isLt2M = file.size / 1024 / 1024 < 2
        // 这里常规检验，看项目需求而定
        if (!isImage) {
          this.$message.error('只能上传图片格式png、jpg、gif!')
          return
        }
        if (!isLt2M) {
          this.$message.error('只能上传图片大小小于2M')
          return
        }
        getOssUploadsParam().then((res) => {
          const form = new FormData()
          form.append('key', res.data.dir + file.name)
          form.append('OSSAccessKeyId', res.data.accessid)
          form.append('policy', res.data.policy)
          form.append('success_action_status', '200')
          form.append('signature', res.data.signature)
          form.append('file', file)
          console.log(222, res)
          axios({
            url: res.data.host,
            method: 'POST',
            data: form,
            processData: false,
            cache: false,
            contentType: false,
          }).then((data) => {
            this.img = res.data.host + '/' + res.data.dir + file.name
            console.log(66, this.img)
          })
        })
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
  }
</style>
